#pragma once

#include "byteStream.hpp"
#include <cstdint>

/* 链路层是 485, 无需预连接 (c-s) */

enum class ClientApduType : uint8_t {
    CONNECT_REQUEST = 2,  /* 建立应用连接请求 */
    RELEASE_REQUEST = 3,  /* 断开应用连接请求 */
    GET_REQUEST = 5,      /* 读取请求 */
    SET_REQUEST = 6,      /* 设置请求 */
    ACTION_REQUEST = 7,   /* 操作请求 */
    REPORT_RESPONSE = 8,  /* 上报应答 */
    PROXY_REQUEST = 9,    /* 代理请求 */
    ERROR_RESPONSE = 110, /* 异常应答 */
};

inline bool operator==(uint8_t v, const ClientApduType &t) {
    return v == static_cast<uint8_t>(t);
}

inline bool operator==(const ClientApduType &t, uint8_t v) {
    return v == t;
}

enum class ServerApduType : uint8_t {
    CONNECT_RESPONSE = 130,     /* 建立应用连接响应 */
    RELEASE_RESPONSE = 131,     /* 断开应用连接响应 */
    RELEASE_NOTIFICATION = 132, /* 断开应用连接通知 */
    GET_RESPONSE = 133,         /* 读取响应 */
    SET_RESPONSE = 134,         /* 设置响应 */
    ACTION_RESPONSE = 135,      /* 操作响应 */
    REPORT_NOTIFICTION = 136,   /* 上报通知 */
    PROXY_RESPONSE = 137,       /* 代理响应 */
    ERROR_RESPONSE = 238,       /* 异常应答 */
};

inline bool operator==(uint8_t v, const ServerApduType &t) {
    return v == static_cast<uint8_t>(t);
}

inline bool operator==(const ServerApduType &t, uint8_t v) {
    return v == t;
}

enum class SecurityApduType : uint8_t {
    SECURITY_REQUEST = 16,   /* 安全请求 */
    SECURITY_RESPONSE = 144, /* 安全响应 */
};

inline bool operator==(uint8_t v, const SecurityApduType &t) {
    return v == static_cast<uint8_t>(t);
}

inline bool operator==(const SecurityApduType &t, uint8_t v) {
    return v == t;
}

class ClientApdu {
protected:
    static constexpr inline uint8_t OadSize = 4;
    ByteStream _stream;

public:
    virtual const ByteStream &frame() const { return _stream; }
    virtual ~ClientApdu() {}
};
